package com.example.bishe.controller.biaoqian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.bishe.entity.Tag;
import com.example.bishe.toolclass.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author yws
 * @Date 2022/4/26 23:23
 */
@RestController
@RequestMapping("/tag")
public class BiaoQianController {
    @Resource
    CUD cud;

    @RequestMapping("/addTag")
    public String addTag(@RequestBody Tag tag){
        int errcode = 0;//错误代码
        String errmsg =" ";//错误描述
        if(tag.getTagname()==null){
            errcode = 1801;
            errmsg = "请传入标签的名称";
        }else {
            HashMap hashMap = cud.addTag(tag.getTagid(), tag.getTagname());
            if (hashMap.get("errcode").equals("0")){
                //插入成功
                errmsg="标签创建成功,标签代码为："+hashMap.get("tagid")+" 标签名称为："+tag.getTagname();
            }else {
                errcode = 1801;
                errmsg = cud.addTagPan(hashMap);
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @RequestMapping("/updateTag")
    public String updateTag(@RequestBody Tag tag){
        int errcode = 0;//错误代码
        String errmsg =" ";//错误描述
        if(tag.getTagid()==null){
            errcode = 1802;
            errmsg = "请输入标签的id";
        }
        else if (tag.getTagname()==null){
            errcode = 1802;
            errmsg = "请输入更新后的标签名称";
        }else {
            HashMap hashMap = cud.updateTag(tag.getTagid(), tag.getTagname());
            if (hashMap.get("errcode").equals("0")){
                //修改标签成功
                errmsg="修改标签"+tag.getTagid()+" 名称为:"+tag.getTagname()+" 成功";
            }else {
                errcode = 1801;
                errmsg = cud.updateTagPan(hashMap);
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @RequestMapping("/deletetag")
    public String deletetag(@RequestBody Tag tag){
        int errcode = 0;//错误代码
        String errmsg =" ";//错误描述
        if(tag.getTagid()==null){
            errcode = 1803;
            errmsg = "请输入标签的id";
        }else {
            HashMap hashMap = cud.deleteTag(tag.getTagid());
            if (hashMap.get("errcode").equals("0")){
                //修改标签成功
                errmsg="删除标签"+tag.getTagid()+" 成功";
            }else {
                errcode = 1803;
                errmsg = cud.deleteTagPan(hashMap);
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @Resource
    BiaoQIan biaoQIan;
    @Resource
    StringTurnJson stringTurnJson;
    @RequestMapping("/getTagUser")
    public String getTagUser(@RequestBody Tag tag){
        int errcode = 0;//错误代码
        String errmsg ="";//错误描述
        Map map = new HashMap();
        if(tag.getTagid()==null){
            errcode = 1804;
            errmsg = "请输入标签的id";
        }else {
            HashMap hashMap = biaoQIan.gitTagUserBytagid(tag.getTagid());
            if (hashMap.get("errcode").equals("0")){
                String tagname = hashMap.get("tagname")+"";
                map.put("tagname", tagname);
                JSONArray jsonArray = JSON.parseArray(hashMap.get("userlist")+"");
                List<TagUser> tagUsers = jsonArray.toJavaList(TagUser.class);
                map.put("userlist", tagUsers);
                JSONArray jsonArrays = JSON.parseArray(hashMap.get("partylist")+"");
                List<Integer> tagUser = jsonArrays.toJavaList(Integer.class);
                map.put("partylist", tagUser);
                errmsg="无错误，已返回信息";
            }else {
                errcode = 1803;
                errmsg = cud.deleteTagPan(hashMap);
            }
        }
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }
    @RequestMapping("/addTagUser")
    /*传入多个用户账号，然后进行添加标签*/
    public String addTagUser(@RequestBody AddTagUser users){
        int errcode = 0;//错误代码
        String errmsg ="";//错误描述
        Map map = new HashMap();
        if (users.getTagid()==null){
            errcode = 1805;
            errmsg ="请最少传递标签id属性";
        }
        else {
            HashMap addTagUser = cud.addTagUser(users);
            if (addTagUser.get("errcode").equals("0")){
                errmsg = "标签已操作成功。";
            }else {
                errcode = 1805;
                errmsg = cud.addTagUserpan(addTagUser);
            }
            map.put("invalidlist", addTagUser.get("invalidlist"));
            map.put("invalidparty", addTagUser.get("invalidparty"));
        }
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }
    @RequestMapping("/deleteTagUser")
    public String deleteTagUser(@RequestBody AddTagUser users){
        int errcode = 0;//错误代码
        String errmsg ="";//错误描述
        Map map = new HashMap();
        if (users.getTagid()==null){
            errcode = 1806;
            errmsg ="请最少传递标签id属性";
        }
        else {
            HashMap addTagUser = cud.deleteTagUser(users);
            if (addTagUser.get("errcode").equals("0")){
                errmsg = "标签已操作成功。";
            }else {
                errcode = 1806;
                errmsg = cud.addTagUserpan(addTagUser);
            }
            map.put("invalidlist", addTagUser.get("invalidlist"));
            map.put("invalidparty", addTagUser.get("invalidparty"));
        }
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }
    @RequestMapping("/selectAllTag")
    public String selectAllTag(){
        int errcode = 0;//错误代码
        String errmsg ="";//错误描述
        List<Tag> list = biaoQIan.biaoQian();
        Map map = new HashMap();
        map.put("taglist",list);
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }




}
